package com.example.demo.util;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


// 主类，用于找出相似图片
public class FindSimilarImages {
    public static void main(String[] args) {
        String inputImagePath = "D:/input/009_11.png";
        String folderPath = "D:/input1";
        double similarityThreshold = 0.8;

        try {
            // 读取输入图片
            BufferedImage inputImage = ImageIO.read(new File(inputImagePath));
            String inputHash = PHash.pHash(inputImage);

            // 获取文件夹中的所有图片文件
            File folder = new File(folderPath);
            File[] files = folder.listFiles((dir, name) ->
                    name.toLowerCase().endsWith(".jpg") || name.toLowerCase().endsWith(".png"));

            if (files == null || files.length == 0) {
                System.out.println("未找到图片文件。");
                return;
            }

            // 遍历文件夹中的图片
            for (File file : files) {
                BufferedImage image = ImageIO.read(file);
                String hash = PHash.pHash(image);
                int distance = HammingDistance.hammingDistance(inputHash, hash);
                double similarity = 1.0 - (double) distance / inputHash.length();
                if (similarity >= similarityThreshold) {
                    System.out.println("相似图片: " + file.getName() + "，相似度: " + similarity * 100 + "%");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
